package org.jeecg.modules.system.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.system.entity.Notice;
import org.jeecg.modules.system.entity.OaLinkmanNotice;
import org.jeecg.modules.system.mapper.NoticeMapper;
import org.jeecg.modules.system.service.INoticeService;
import org.jeecg.modules.system.vo.JournalVO;
import org.jeecg.modules.system.vo.ReportWeekPlanVO;
import org.jeecg.modules.system.vo.ReportWeekVO;
import org.jeecg.modules.system.vo.SystemMsgRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 推送
 * @Author: jeecg-boot
 * @Date:   2020-02-23
 * @Version: V1.0
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public List<Notice> findUnReads(String userId) {
        return noticeMapper.findUnReads(userId);
    }

    @Override
    public List<Notice> findAllReads(String userId) {
        return noticeMapper.findAllReads(userId);
    }

    @Override
    public int findUnreadsByType(String userId, String msgType) {
        return noticeMapper.findUnreadsByType(userId,msgType);
    }

    @Override
    public IPage<Notice> selectSqlList(Page<Notice> page, SystemMsgRequestVO msgRequestVO) {
        return page.setRecords(noticeMapper.selectSqlList(page,msgRequestVO));
    }

    @Override
    public JournalVO getJournalById(String journalId) {
        return noticeMapper.getJournalById(journalId);
    }

    @Override
    public ReportWeekVO getReportWeekById(String reportWeekId) {
        ReportWeekVO results = noticeMapper.getReportWeekById(reportWeekId);
        if (results == null) {
            return null;
        }
        checkReportWeekVersion(results);
        return results;
    }

    private void checkReportWeekVersion(ReportWeekVO results) {
        if (StringUtils.isBlank(results.getWorkContent())) {
            results.setNewWorkType(1);
            results.setWorkContent("");
        }
        if (StringUtils.isBlank(results.getNextPlan())) {
            results.setNextPlan("");
        }
        List<ReportWeekPlanVO> thisWeekList = Lists.newArrayList();// 本周完成事项
        List<ReportWeekPlanVO> nextWeekList = Lists.newArrayList();// 下周计划事项
        List<ReportWeekPlanVO> sList = noticeMapper.getReportWeekPlanByUserId(results); // 获取当前周报的创建者上周的计划
        if (sList.size() > 0) {
            thisWeekList.addAll(sList);
            for (int i = 0; i < sList.size(); i++) {
                results.setWorkContent(results.getWorkContent() + sList.get(i).getWeekPlan() + "</br>");
            }
        }
        if (StringUtils.isNotBlank(results.getId())) {
            List<ReportWeekPlanVO> reportWeekPlanList = noticeMapper.getReportWeekPlanById(results.getId()); // 获取到这周的任务和下周计划
            if (reportWeekPlanList.size() > 0) {
                for (int i = 0; i < reportWeekPlanList.size(); i++) {
                    if (reportWeekPlanList.get(i).getPlanStatus().equals("0")) {// 本周的计划
                        thisWeekList.add(reportWeekPlanList.get(i));
                        results.setWorkContent(
                                results.getWorkContent() + reportWeekPlanList.get(i).getWeekPlan() + "</br>");
                    } else {
                        nextWeekList.add(reportWeekPlanList.get(i));
                        results.setNextPlan(results.getNextPlan() + reportWeekPlanList.get(i).getWeekPlan() + "</br>");
                    }
                }
            }
            results.setThisWeekList(thisWeekList);
            results.setNextWeekList(nextWeekList);
        }
    }
}
